package com.augurit.wf.mgmt.convert;


import java.text.DateFormat;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import com.augurit.wf.common.entity.Jbpm4Variable;
import com.augurit.wf.mgmt.web.form.Jbpm4VariableForm;

public class Jbpm4VariableConvertor {
	
	//将form转化为entity
	public static void convertToVo(Jbpm4VariableForm form, Jbpm4Variable entity){
		if(form != null && entity != null){
			entity.setDbid(form.getDbid());
			entity.setClazz(form.getClazz());
			entity.setDbversion(form.getDbversion());
			entity.setKey(form.getKey());
			entity.setConverter(form.getConverter());
			entity.setHist(form.getHist());
			entity.setExecutionDbid(form.getExecutionDbid());
			entity.setTaskId(form.getTaskId());
			entity.setLob(form.getLob());
			entity.setDateValue(form.getDateValue());
			entity.setDoubleValue(form.getDoubleValue());
			entity.setClassName(form.getClassName());
			entity.setLongValue(form.getLongValue());
			entity.setStringValue(form.getStringValue());
			entity.setClobValue(form.getClobValue());
			entity.setExesys(form.getExesys());
		}
	}
	
	public static List<Jbpm4VariableForm> convertEntityListToFormList(List<Jbpm4Variable> entityList){
		List<Jbpm4VariableForm> formList = null;
		if(entityList != null && entityList.size() > 0){
			formList = new ArrayList<Jbpm4VariableForm>();
			
			for(Jbpm4Variable entity :entityList){
				Jbpm4VariableForm form = new Jbpm4VariableForm(entity);
				formList.add(form);
			}
		}
		return formList;
	}
	
	//根据form的clazz属性将form中的value转化为对应的整型、字符串、日期、浮点数类型的值
	public static void ConvertFormValue(Jbpm4VariableForm form){
		if(form.getClazz() != null){
			if(form.getClazz().equals("string"))
				form.setStringValue(form.getValue());
			else if(form.getClazz().equals("long"))
				form.setLongValue(Long.parseLong(form.getValue()));
			else if(form.getClazz().equals("date"))
				try {
					form.setDateValue(DateFormat.getDateInstance().parse(form.getValue()));
				} catch (ParseException e) {
					e.printStackTrace();
				}
			else if(form.getClazz().equals("double"))
				form.setDoubleValue(Double.parseDouble(form.getValue()));
		}
	}
}
